<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="登录账户设置" name="first">
            <el-form
              ref="userInfoRef"
              :model="userInfoForm"
              :rules="userInfoRules"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input
                  v-model="userInfoForm.username"
                  style="width: 300px"
                />
              </el-form-item>
              <el-form-item label="密码:">
                <el-input
                  v-model="userInfoForm.password"
                  style="width: 300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="primary"
                  @click="updateUser"
                >更新</el-button>
                <el-button type="text" @click="$router.back()">取消</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情" name="second">
            <component :is="userComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="third">
            <component :is="jobInfo" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import userComponent from '../components/user-info'
import jobInfo from '../components/job-info'

export default {
  name: 'EmployeeDetail',
  data() {
    return {
      userComponent,
      jobInfo,
      userId: this.$route.params.id,
      activeName: 'first',
      userInfoForm: {
        username: '',
        password2: ''
      },
      userInfoRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.loadUserInfo()
  },
  methods: {
    async loadUserInfo() {
      this.userInfoForm = await getUserDetailById(this.userId)
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    async updateUser() {
      this.$refs.userInfoRef.validate(async isOK => {
        if (isOK) {
          await saveUserDetailById({
            ...this.userInfoForm,
            password: this.userInfoForm.password2
          })
          this.$message.success('更新用户资料成功')
          this.userInfoForm = {}
          this.loadUserInfo()
        }
      })
    }
  }
}
</script>

<style></style>
